package com.hgkj.cloud.base.scheme.table;

import com.hgkj.cloud.base.scheme.ClsAnswerRequest;
import com.hgkj.cloud.base.scheme.ClsAnswerResult;
import com.hgkj.cloud.base.scheme.ClsScore;

import java.util.Map;

/**
 * 二十、流调用抑郁自评量表（Ces）共20题
 */
public class Table20 extends AbstractTable implements TableCalculate{
    String code = "Ces";

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public ClsAnswerResult calc(ClsAnswerRequest answer) {
        return GetCesFactorScore(answer);
    }

    @Override
    public String[] report(Map map) {
        return null;
    }

    private static ClsAnswerResult GetCesFactorScore(ClsAnswerRequest answer) {
        // 总分
        ClsAnswerResult result = new ClsAnswerResult();
        result.setMessage("");
        result.setType(answer.getType());
        result.setUuid(answer.getUuid());
        try {
            // 1正确题数
            int MAX_NUM = 20;

            int intI;
            String[] strAnswer_c = answer.getAnswer().split(g_sChar);
            if (strAnswer_c.length > 1) {
                for (intI = 1; intI < strAnswer_c.length; intI++) {
                    strAnswer_c[intI] = strAnswer_c[intI].trim();
                }
            }

            String[] sAnswer = strAnswer_c[0].toString().split(g_nChar);

            if (sAnswer.length != MAX_NUM) {
                result.setMessage("答案数量不符！");
                return result;
            }

            int i, j;

            // 共二组，第一组0,1,2,3，第二组3,2,1,0
            String[] strNumberA = { "1,2,3,5,6,7,9,10,11,13,14,15,17,18,19,20", "4,8,12,16" };
            String[] strSubA;
            int[][] intScoreA = { { 0, 1, 2, 3 }, { 3, 2, 1, 0 } };

            int vFactor = 0; // 实际为1个因子

            // 总分
            for (i = 0; i < strNumberA.length; i++) {
                strSubA = strNumberA[i].split(",");
                for (j = 0; j < strSubA.length; j++) {
                    vFactor += intScoreA[i][Integer.valueOf(sAnswer[Integer.valueOf(strSubA[j]) - 1]) - 1];
                }
            }

            // 按“总分”得到“诊断结果”
            // 按“总分”得到“指导意见”
            int[] intValueA = { 15, 19 };
            String[] strValueA = { "无抑郁症状", "可能有抑郁症状", "有抑郁症状" };
            String vAppraise = strValueA[strValueA.length - 1];
            String vDoctorIdea = "Ces.1.3";

            for (i = 0; i < intValueA.length; i++) {
                if (vFactor <= intValueA[i]) {
                    vAppraise = strValueA[i];
                    vDoctorIdea = "Ces.1." + String.valueOf(i + 1);
                    break;
                }
            }

            ClsScore.Ces ces = new ClsScore.Ces();

            ces.setTotal_o(vFactor);
            ces.setTotal_a(vAppraise);
            ces.setTotal_d(vDoctorIdea);

            result.setObject(ces);
            return result;
        } catch (ArithmeticException e) {
            result.setMessage(e.toString());
            return result;
        }
    }


}
